#import "SEGQueryneighborhoodmoreManager.h"

#if __has_include(<Commom/SEGModel.h>)
#import <Commom/SEGModel.h>
#else
#import "SEGModel.h"
#endif
#if __has_include(<Commom/SEGHTTPBusinessManager.h>)
#import <Commom/SEGHTTPBusinessManager.h>
#else
#import "SEGHTTPBusinessManager.h"
#endif

#if __has_include(<SEGPlatform/SEGBussinessConstHeader.h>)
#import <SEGPlatform/SEGBussinessConstHeader.h>
#else
#import "SEGBussinessConstHeader.h"
#endif

#if __has_include(<Commom/SEGProfileManager.h>)
#import <Commom/SEGProfileManager.h>
#else
#import "SEGProfileManager.h"
#endif

#if __has_include(<SEGPlatform/SEGPlatformManager.h>)
#import <SEGPlatform/SEGPlatformManager.h>
#else
#import "SEGPlatformManager.h"
#endif

#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif


static id _QueryNeighborhoodMoreManager;
static dispatch_once_t onceToken;

@implementation SEGQueryneighborhoodmoreManager
#pragma mark - SINGLE----BEGIN----
+(instancetype)sharedManager{
    dispatch_once(&onceToken, ^{
        _QueryNeighborhoodMoreManager = [[self alloc] init];
    });
    return _QueryNeighborhoodMoreManager;
}

+(void)destroyResource{
    onceToken = 0;
    _QueryNeighborhoodMoreManager = nil;
}

#pragma mark - SINGLE----END----

#pragma mark - HTTP----BEGIN----
-(void)requestNeighborhoodWithScopeId:(NSString *)scopeId
                                objId:(NSString *)objId
                               cityId:(NSString *)cityId
                              objType:(NSString *)objType
                               pageNo:(NSString *)pageNo
                            pageLimit:(NSString *)pageLimit
                          communityId:(NSString *)communityId
                             moduleId:(NSString *)moduleId
                             finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:scopeId, @"scopeId", pageNo, @"pageNo", pageLimit, @"pageLimit", nil];
    if (objId != nil && objType != nil) {
        [params setObject:objId forKey:@"objId"];
        [params setObject:objType forKey:@"objType"];
    }
 
    NSString *urlString = nil;
    urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kCMS_api,@"/api/v2/queryNeighborhoodMore",nil];
    if (communityId && communityId.length > 0) [params setObject:communityId forKey:@"communityId"];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
        if (!isSuccessed) {
            finished(isSuccessed,nil,codeType,error.localizedDescription);
            return;
        }
        
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO,nil, codeType, error.localizedDescription);
            return;
        }
        
        NSArray* list = [dicData valueForKey:@"neighborhood"];
        
        if (!list || ![list isKindOfClass:[NSArray class]]) {
            finished(NO,nil, codeType, error.localizedDescription);
            return;
        }
        
        if ([pageNo integerValue] == 1) { //顶部刷新
            self.neighborhoodListArray = [NSMutableArray array];
        }
        
        for (NSDictionary* dic in list) {
            SEGSharedNeihborhoodModel* _model = [SEGSharedNeihborhoodModel initWithDictionary:dic];
            __block BOOL isNotLike = NO;
            //本地过滤不感兴趣的数据
            NSDictionary *tempDict = [[NSUserDefaults standardUserDefaults] objectForKey:kNeighborhoodNotLikeArray];
            if (tempDict && [tempDict isKindOfClass:[NSDictionary class]]) {
                __block NSArray *array;
                if ([tempDict objectForKey:kAccountUserID]) {
                    array = [tempDict objectForKey:kAccountUserID];
                }
                if (array && [array isKindOfClass:[NSArray class]]) {
                    [array enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL * _Nonnull stop) {
                        NSString *objType = ValueToString(dic[@"objType"]);
                        NSString *objId = ValueToString(dic[@"objId"]);
                        if([objType isEqualToString:_model.model.objType] && [objId isEqualToString:_model.model.objId]) {
                            isNotLike = YES;
                            *stop = YES;
                        }
                    }];
                }
            }
            __block BOOL isReported = NO;
            //本地过滤举报过的数据
            tempDict = [[NSUserDefaults standardUserDefaults] objectForKey:kNeighborhoodReportedArray];
            if (tempDict && [tempDict isKindOfClass:[NSDictionary class]]) {
                __block NSArray *array;
                if ([tempDict objectForKey:kAccountUserID]) {
                    array = [tempDict objectForKey:kAccountUserID];
                }
                if (array && [array isKindOfClass:[NSArray class]]) {
                    [array enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL * _Nonnull stop) {
                        NSString *objType = ValueToString(dic[@"objType"]);
                        NSString *objId = ValueToString(dic[@"objId"]);
                        if([objType isEqualToString:_model.model.objType] && [objId isEqualToString:_model.model.objId]) {
                            isReported = YES;
                            *stop = YES;
                        }
                    }];
                }
            }
            
            if ((!isNotLike) && (!isReported)) {
                [self.neighborhoodListArray addObject:_model];
            }
        }
        
        self.tips = [dicData valueForKey:@"tips"];
        finished(isSuccessed,result,codeType,error.localizedDescription);
        return;
    }];
}

-(void)requestAllQueryCommentsWithObjId:(NSString*)objId objType:(NSString*)objType finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:objId forKey:@"objId"];
    [params setValue:objType forKey:@"objType"];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kCMS_api,@"/api/v2/queryComments",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
        if (!isSuccessed) {
            finished(isSuccessed,nil,codeType,error.localizedDescription);
            return;
        }
        
        NSDictionary* dicData = [result valueForKey:@"data"];
        if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
            finished(NO,nil, codeType, error.localizedDescription);
            return;
        }
        
        self.childCommentsArray = [NSMutableArray array];
        NSArray* list = [dicData valueForKey:@"comments"];
        
        if (list && [list isKindOfClass:[NSArray class]]) {
            for (NSDictionary* dic in list) {
                SEGSharedCommentChildCommentModel* _model = [SEGSharedCommentChildCommentModel initWithDictionary:dic];
                [self.childCommentsArray addObject:_model];
            }
        }
        finished(isSuccessed,result,codeType,error.localizedDescription);
        return;
    }];
}

#pragma mark - HTTP----END----

#pragma mark - GETTER----BEGIN----
-(NSMutableArray<SEGSharedNeihborhoodModel *> *)neighborhoodListArray{
    if (!_neighborhoodListArray) {
        _neighborhoodListArray = [NSMutableArray array];
    }
    return _neighborhoodListArray;
}

-(NSMutableArray<SEGSharedCommentChildCommentModel *> *)childCommentsArray{
    if (!_childCommentsArray) {
        _childCommentsArray = [NSMutableArray array];
    }
    return _childCommentsArray;
}
#pragma mark - GETTER----END----
@end

